package me.zhengjie.server.impl;

import cn.hutool.core.lang.func.VoidFunc0;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.dao.*;
import me.zhengjie.mapper.OutboundOrderDetailMapper;
import me.zhengjie.mapper.OutboundOrderMapper;
import me.zhengjie.mapper.TransferOrderMapper;
import me.zhengjie.mapper.TransferOutboundDetailMapper;
import me.zhengjie.server.TransferOrderService;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author xinglin.lu
* @description 针对表【iws_transfer_order】的数据库操作Service实现
* @createDate 2025-06-10 13:39:26
*/
@Service
public class TransferOrderServiceImpl extends ServiceImpl<TransferOrderMapper, TransferOrderDao>
    implements TransferOrderService{

    @Autowired
    private OutboundOrderMapper outboundOrderMapper;

    @Autowired
    private TransferOutboundDetailMapper transferOutboundDetailMapper;

    @Override
    public PageResult<TransferOrderDao> queryAll(TransferOrderDao criteria, Page<Object> page) {
        List<TransferOrderDao> resultList = outboundOrderMapper.selectListForTransferOrder(criteria);
        for (TransferOrderDao transferOrderDao : resultList) {
            List<TransferOutboundDetailDao> transferOutboundDetailDaos =
                    transferOutboundDetailMapper.selectList(new LambdaQueryWrapper<TransferOutboundDetailDao>().eq(TransferOutboundDetailDao::getTransferCode, transferOrderDao.getCode()));
            List<String> collect = transferOutboundDetailDaos.stream().filter(e->e.getOutboundCode()!=null).map(TransferOutboundDetailDao::getOutboundCode)
                    .map(code -> code.replaceAll("\\r\\n","")).collect(Collectors.toList());
            transferOrderDao.setOutboundCodes(collect);
        }
        if (resultList!=null){
            PageUtil.paging(criteria.getPage(),criteria.getSize(),resultList);
        }
        return PageUtil.toPage(resultList);
    }

    @Override
    public TransferOrderDetailDao queryDetail(String transferCode) {
        TransferOrderDetailDao dao = new TransferOrderDetailDao();
        List<TransferOrderDetailListDao> resultList = outboundOrderMapper.selectListForTransferOrderDetail(transferCode);

        if (resultList!=null&&resultList.size()>0){
            dao.setCode(resultList.get(0).getCode());
            dao.setNewCode(resultList.get(0).getNewCode());
            dao.setPlant(resultList.get(0).getPlant());
            dao.setDetailLists(resultList);
        }
        List<TransferOrderCodeListDao> codeListDaos = outboundOrderMapper.selectForHostoryOrder(transferCode);
        codeListDaos.stream().filter(e->e.getOutboundCode()!=null).forEach(e->e.getOutboundCode().replaceAll("\\r\\n",""));
        if (codeListDaos!=null){
            dao.setHistoryLists(codeListDaos);
        }
        return dao;
    }

    @Override
    public Object updateOutBandCodeById(ClosedOutBoundOrderDao resources) {
        /*1 判断任务是否正在进行中
          2 如果正在进行则不能取消
          3 没有进行的任务取消任务
         */
        for (String code : resources.getCodes()) {
            TransferHandDao v0 =outboundOrderMapper.selectOutBandStatus(code);
            if (v0==null){
                throw new RuntimeException("没有此出库单号信息！");
            }
            if (v0.getStatus()!=0){
                //不能取消出库单
            }else {
                outboundOrderMapper.updateOutBand(code);
                //取消出库单号
            }
        }

        return true;
    }

    @Override
    public Object handClosedTransferOrder(ClosedTransferOrderOrderDao resources) {
        for (String code : resources.getCodes()) {
            TransferOutboundDetailDao transferOutboundDetailDao = transferOutboundDetailMapper.selectOne(new LambdaQueryWrapper<TransferOutboundDetailDao>().eq(TransferOutboundDetailDao::getTransferCode, code));
            if (transferOutboundDetailDao!=null){
                throw new RuntimeException("已经关联不能取消！");
            }

        }

        return true;
    }

    @Override
    public Object updateForStatus(TransferOrderDao resources) {
        int i =baseMapper.updateForStatus(resources);
        return i>0?true:false;
    }
}




